package com.erci.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class WebMvcConfig {

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();

        // 允许的源
        config.addAllowedOrigin("http://localhost:5172");
        config.addAllowedOrigin("http://localhost:5173");
        // 允许的头
        config.addAllowedHeader("*");
        // 允许的方法
        config.addAllowedMethod("*");
        // 暴露的头
        config.addExposedHeader("Authorization");
        // 是否允许凭证
        config.setAllowCredentials(false);

        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}
